#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using i128 = __int128;

const int B = 1000;
struct Block {
  Block() : sum(0) {}

  void add(int v) {
    val.emplace_back(v);
  }

  void sort() {
    std::sort(val.begin(), val.end());
    ll s = 0;
    for (auto &x : val) {
      s += x;
      sum.emplace_back(s);
    }
  }

  int size() {
    return (int)val.size();
  }

  ll query (Block &p) {
    ll res = 0;
    int j = 0;
    ll s = 0, c = 0;
    for (auto &v : val) {
      while (j < p.size() && p.val[j] < v) {
        c += 1;
        s += p.val[j++];
      }
      res += 1ll * v * c - s + (p.sum.back() - s) - (p.size() - c) * v;
    }
    return res;
  }

  ll query(int v) {
    auto it = lower_bound(val.begin(), val.end(), v);
    int j = it - val.begin();
    if (it == val.end()) {
      j = size();
    }
    j--;
    if (j < 0) {
      return sum.back() - 1ll * v * size();
    }
    return 1ll * (j + 1) * v - sum[j] + (sum.back() - sum[j]) - 1ll * (size() - j - 1) * v;
  }

  vector<int> val;
  vector<ll> sum;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  
  vector<int> a(n), b(n), bel(n);
  
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }

  vector<Block> va, vb;
  int cor = 0;
  for (int i = 0; i < n; i += B) {
    int r = min(i + B, n);
    Block ta, tb;
    for (int j = i; j < r; j++) {
      bel[j] = cor;
      ta.add(a[j]); tb.add(b[j]);
    }
    ta.sort(); tb.sort();
    va.emplace_back(ta);
    vb.emplace_back(tb);
    cor++;
  }

  vector<vector<ll>> suma(n, vector<ll>(cor)), sumb(n, vector<ll>(cor));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < cor; j++) {
      suma[i][j] = vb[j].query(a[i]);
      sumb[i][j] = va[j].query(b[i]);
    }
  }

  for (int i = 0; i < cor; i++) {
    for (int j = 1; j < n; j++) {
      suma[j][i] += suma[j - 1][i];
      sumb[j][i] += sumb[j - 1][i];
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 1; j < cor; j++) {
      suma[i][j] += suma[i][j - 1];
      sumb[i][j] += sumb[i][j - 1];
    }
  }

  int q;
  cin >> q;

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    int bl = bel[l] - 1, br = bel[r] - 1;
    ll ans = 0;
    int pl = max(0, (bl + 1) * B), pr = max(0, (br + 1) * B);
    Block ta, tb;
    for (int j = pl; j <= l; j++) {
      ta.add(a[j]);
    }
    for (int j = pr; j <= r; j++) {
      tb.add(b[j]);
    }
    ta.sort(); tb.sort();
    if (br >= 0) {
      ans += suma[l][br];
    }
    if (bl >= 0) {
      ans += sumb[r][bl] - (pr - 1 >= 0 ? sumb[pr - 1][bl] : 0);
    }
    ans += ta.query(tb);
    cout << ans << '\n';
  }
  return 0;
}